<?php
require_once('Vega/Dao/Table.php');
require_once 'Mobitv/Commons/Cache.php';
class Mobitv_Admin_MenuDao extends Vega_Dao_Table {

    public function getGroupPerm($menuId) {
        $menuId = intval($menuId);
        $sql = "select ag.id, ag.name, agm.permission from admin_group ag left join admin_group_menu agm oN ag.id=agm.group_id AND agm.menu_id=$menuId;";
        $this->_db->query($sql);
        return $this->_db->fetchRowSet();
    }

    public function deleteFromList($list) {
        $list = "(" . implode(",", $list) . ")";
        $sql1 = "DELETE FROM menu WHERE id IN $list";
        $sql2 = "DELETE FROM admin_group_menu WHERE menu_id IN $list";
        $ret = $this->_db->queryWrite($sql1);
        if ($ret) $this->_db->queryWrite($sql2);
        return $ret;
    }

    public function add($data) {
        $title = $this->valueQuote($data['title']);
        $url = $this->valueQuote($data['url']);
        $parentId = $this->valueQuote($data['parent_id']);
        $sql = "INSERT INTO menu SET title=$title, url=$url, parent_id=$parentId";
        $ret = $this->_db->queryWrite($sql);
        if (!$ret) return false;

        $this->_db->query("SELECT MAX(id) as id FROM menu", 'master');
        $row = $this->_db->fetchRow();
        $menuId = $row['id'];
        $ret = false;
        foreach ($data['group_perm'] as $pair) {
            $groupId = $pair['group_id'];
            $permission = $pair['permission'];
            $ret2 = $this->_db->queryWrite("INSERT INTO admin_group_menu SET menu_id=$menuId, group_id=$groupId, permission=$permission ON DUPLICATE KEY UPDATE permission=$permission");
            $ret = $ret || $ret2;
        }
        return $ret;
    }
    
    public function save($menuId, $data) {
        $title = $this->valueQuote($data['title']);
        $url = $this->valueQuote($data['url']);
        $parentId = $this->valueQuote($data['parent_id']);

        $ret = false;
        foreach ($data['group_perm'] as $pair) {
            $groupId = $pair['group_id'];
            $permission = $pair['permission'];
            $sql = "INSERT INTO admin_group_menu SET menu_id=$menuId, group_id=$groupId, permission=$permission ON DUPLICATE KEY UPDATE permission=$permission";
            $ret2 = $this->_db->queryWrite($sql);
            $ret = $ret || $ret2;
        }
        
        $sql = "UPDATE menu SET title=$title, url=$url, parent_id=$parentId WHERE id=$menuId";
        $ret2 = $this->_db->queryWrite($sql);
        return $ret || $ret2;
    }

    public function getDetail($menuId) {
        $menuId = intval($menuId);
        $sql = "SELECT * FROM menu WHERE id=$menuId";
        $this->_db->query($sql);
        return $this->_db->fetchRow();
    }
    
    public function getIdList() {
        $sql = 'SELECT id FROM menu';
        $this->_db->query($sql);
        return $this->_db->fetchRowSet();
    }

    public function getList() {
        $arrParentMenu = array();
        $sql = "SELECT * FROM menu ORDER BY `order`";
        $this->_db->query($sql);
        $rows = $this->_db->fetchRowSet();
        foreach ($rows as $row) {
            $parentId = $row['parent_id'];
            $id = $row['id'];
            if ($parentId == 0) {
                $arrParentMenu['p' . $id] = array();
                $arrParentMenu['p' . $id]['self'] = $row;
                $arrParentMenu['p' . $id]['children'] = array();
            } else {
                continue;
            }
        }

        foreach ($rows as $row) {
            $parentId = $row['parent_id'];
            if ($parentId == 0) continue;
            if (isset($arrParentMenu['p' . $parentId])) {
                $arrParentMenu['p' . $parentId]['children'][] = $row;
            }

        }

        return $arrParentMenu;
    }

    public function loadMenu($groupId = -1) {
        if ($groupId == -1) $groupId = isset($_SESSION['groupid']) ? $_SESSION['groupid'] : 0;
        $useid = $_SESSION['userid'];
        $arrParentMenu = array();
        $sql = "SELECT m.* FROM menu m
                    INNER JOIN admin_group_menu agm ON m.id=agm.menu_id
                    INNER JOIN admin_access_role r ON r.group_id = agm.group_id
                    WHERE agm.group_id=$groupId AND agm.permission=1 AND r.admin_id = $useid
                    ORDER BY `order`";
        $sql = "SELECT m.* FROM menu m
                    INNER JOIN admin_group_menu agm ON m.id=agm.menu_id
                    WHERE agm.group_id=$groupId AND agm.permission=1
                    ORDER BY `order`";
        $this->_db->query($sql);
        $rows = $this->_db->fetchRowSet();
        foreach ($rows as $row){
            $parentId = $row['parent_id'];
            $id = $row['id'];
            if ($parentId == 0) {
                $arrParentMenu['p' . $id] = array();
                $arrParentMenu['p' . $id]['self'] = $row;
                $arrParentMenu['p' . $id]['children'] = array();
            } else {
                continue;
            }
        }

        foreach ($rows as $row) {
            $parentId = $row['parent_id'];
            if ($parentId == 0) continue;
            if (isset($arrParentMenu['p' . $parentId])) {
                $arrParentMenu['p' . $parentId]['children'][] = $row;
            }

        }

        /*$cache->save($cacheKey, $arrParentMenu, 15 * 60); // cache 15 minutes*/
        return $arrParentMenu;
    }
}